package com.hdax.dm.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdax.dm.config.AlipayConfig;
import com.hdax.dm.entity.order.DmOrder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping(path = "/pay")
public class PayController {

    @PostMapping(path = "/alipay")
    public void alipay(HttpServletResponse response, @RequestBody Map<String, String> params) throws Exception{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //根据请求订单号 ，查询订单信息
        String orderNo = params.get("orderNo");
        //远程调用订单服务，查询订单详情 ： orderNo itemName totalAmount
        DmOrder dmOrder = new DmOrder();
        dmOrder.setOrderNo(orderNo);
        dmOrder.setTotalAmount(416L);
        dmOrder.setItemName("八喜2018年第七届打开艺术之门暑期系列演出意大利奇妙人声秀MEZZOTONO（无锡站）");
        //商户订单号，商户网站订单系统中唯一订单号，必填
//        String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//        //付款金额，必填
//        String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
//        //订单名称，必填
//        String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
        //商品描述，可空
        String body = new String("");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ dmOrder.getOrderNo() +"\","
                + "\"total_amount\":\""+ dmOrder.getTotalAmount() +"\","
                + "\"subject\":\""+ dmOrder.getItemName() +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //输出
        out.print(result);
    }

    @PostMapping(path = "introduction")
    public void introduction(HttpServletRequest request){
        //支付宝服务器通过异步请求url向商家发送了支付成功确认的消息
        //服务可以在此处完成订单的确认 0->2
        System.out.println("支付宝订单号："+request.getParameter("trade_no"));
        System.out.println("商家订单号："+request.getParameter("out_trade_no"));
        System.out.println("买家支付宝id："+request.getParameter("buyer_id"));
        System.out.println("交易状态："+request.getParameter("trade_status"));
        System.out.println("实收金额："+request.getParameter("receipt_amount"));
        System.out.println("用户支付金额："+request.getParameter("buyer_pay_amount"));
        System.out.println("交易付款时间："+request.getParameter("gmt_payment"));
        System.out.println("");
    }
}
